package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.*;
import com.woniuxy.controller.info.Showpageinof;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ParameterException;
import com.woniuxy.exception.deleShopIsNULL;
import com.woniuxy.model.PawnShop;
import com.woniuxy.service.PawnShopService;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Api(tags = "店铺相关接口")
@RestController
@RequestMapping("/pawnShop")
public class PawnShopController {
    @Resource
    private PawnShopService pawnShopService;


    @ApiOperation("没有分页获取所有店铺")
    @GetMapping("/getAllshop")
    public Result findAlshop(){
        return Result.success(200,"查询成功",pawnShopService.list());
    }

    /**
     *查询所有门店渲染借口，
     * 分页查询
     */
    @ApiOperation("分页获取所有店铺")
    @GetMapping("/findAllshop")
    public Result getAllShopsPage(FindAllshopsform findAllshops ){

        return pawnShopService.findpageshop(findAllshops);
    }


    /*
    根据条件查询门店byLike
    */
    @ApiOperation("模糊分页获取所有店铺")
    @GetMapping("/getShopByLike")
    public Result getShopByLike(FindShopByLikefrom findShopByLikefrom){

        return pawnShopService.findShoppageBylike(findShopByLikefrom);
    }

    /*
    * 根据id 批量删除门店
    * */
    @ApiOperation("批量删除门店店铺")
    @PostMapping("/delShopById")
    public Result delShopById(@RequestBody DelShopByIdform delShopByIdform) throws deleShopIsNULL {

        return pawnShopService.deleshops(delShopByIdform);
    }

    /**
     * 新增门店
     *       以门店相关联的仓库
     * @return
     */
    @ApiOperation("新增删除门店店铺")
    @PostMapping("/addShop")
    public Result addShop(@RequestBody @Valid AddShopform addShopform,
                          BindingResult bindingResult) throws DdException {

        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            String s="";
            for (ObjectError allError : allErrors) {
                s = allError.getDefaultMessage();
            }
            throw new ParameterException(500003, s);
        }

     return pawnShopService.addshop(addShopform);

    }

    /*
    *更改门店信息
     * */
    @ApiOperation("修改门店店铺")
    @PostMapping("/updateShop")
    public Result updateShop(@RequestBody UpateShopform upateShopform ){

        Shopupdateform shopform = upateShopform.getShopform();
        System.out.println(shopform);
        PawnShop pawnShop = new PawnShop(shopform.getShopCode(), shopform.getShopName(), shopform.getContact(), shopform.getPhoneNo(), shopform.getAddress(), shopform.getShopStat());
//        PawnShop pawnShop = NewCopyBean.copyOne(shopform, PawnShop.class);
//        System.out.println(pawnShop);
        pawnShopService.update(pawnShop,new QueryWrapper<PawnShop>().eq("shop_code",shopform.getShopCode()));
        return Result.success(200,"修改成功",upateShopform);
    }

    @ApiOperation("根据门店id找店铺")
    @GetMapping("/getShopById")
    @ResponseBody
    public Result getShopById(Integer shopCode){
        PawnShop result = pawnShopService.getById(shopCode);
        return Result.success(200,"getShopById成功",result);
    }
}

